package com.scrumtrek.trainings.tdd;

import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: skashin
 * Date: 21.02.13
 * Time: 14:14
 * To change this template use File | Settings | File Templates.
 */
public class StoreStub implements IStore {

    public int getTotalItemsAmountByType(Class clazz) {
        return getItemsByType(clazz).size();
    }

    public void addItem(Item item) {
        Db.getInstance().addItem(item);
    }

    public Set<Item> getItemsByTitle(String titleName) {
        Set<Item> items = new HashSet<Item>();
        for(Item item : Db.getInstance().getItems()){
            if(item.getTitle().equals(titleName)){
                items.add(item);
            }
        }

        return items;
    }

    public Set<String> getFormattedTitlesByType(Class<Book> clazz) {
        Set<String> formattedTitles = new HashSet<String>();

        for(Item item : getItemsByType(clazz)){
            formattedTitles.add(item.getFormattedTitle());
        }

        return formattedTitles;
    }

    public Set<Item> getItemsByType(Class clazz) {
        Set<Item> items = new HashSet<Item>();
        for(Item item : Db.getInstance().getItems()){
            if(item.getClass().equals(clazz)){
                items.add(item);
            }
        }
        return items;
    }




    public void sell(Item item) {
        try{
            Db.getInstance().getItems().remove(item);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("can't profit external system");
        }

    }

}
